package com.main.locatme;

import com.locatme.model.Langue;
import com.locatme.model.Enseigne;
import com.locatme.modelBD.LangueBD;
import com.locatme.modelBD.UtilisateurBD;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class CreateEnseigneActivity extends Activity {


    private UtilisateurBD userBD;
    private LangueBD langueBD;
    private Langue langue;
    private Enseigne user;



    private  EditText loginEditText ;
    private  EditText passEditText1;
    private  EditText passEditText2;
    private  EditText nomEnseigne;
    private EditText descEnseigne;
    private EditText houverture;
    private EditText hfermeture;
    private  Spinner spinLangue;

    public Langue getLangue() {
        return langue;
    }

    public void setLangue(Langue langue) {
        this.langue = langue;
    }

    public UtilisateurBD getUserBD() {
        return userBD;
    }

    public void setUserBD(UtilisateurBD userBD) {
        this.userBD = userBD;
    }

    public LangueBD getLangueBD() {
        return langueBD;
    }

    public void setLangueBD(LangueBD langueBD) {
        this.langueBD = langueBD;
    }

    public Enseigne getUser() {
        return user;
    }

    public void setUser(Enseigne user) {
        this.user = user;
    }

    public EditText getLoginEditText() {
        return loginEditText;
    }

    public void setLoginEditText(EditText loginEditText) {
        this.loginEditText = loginEditText;
    }

    public EditText getPassEditText1() {
        return passEditText1;
    }

    public void setPassEditText1(EditText passEditText1) {
        this.passEditText1 = passEditText1;
    }

    public EditText getPassEditText2() {
        return passEditText2;
    }

    public void setPassEditText2(EditText passEditText2) {
        this.passEditText2 = passEditText2;
    }

    public EditText getNomEnseigne() {
        return nomEnseigne;
    }

    public void setNomEnseigne(EditText nomEnseigne) {
        this.nomEnseigne = nomEnseigne;
    }

    public EditText getDescEnseigne() {
        return descEnseigne;
    }

    public void setDescEnseigne(EditText descEnseigne) {
        this.descEnseigne = descEnseigne;
    }

    public EditText getHouverture() {
        return houverture;
    }

    public void setHouverture(EditText houverture) {
        this.houverture = houverture;
    }

    public EditText getHfermeture() {
        return hfermeture;
    }

    public void setHfermeture(EditText hfermeture) {
        this.hfermeture = hfermeture;
    }

    public Spinner getSpinLangue() {
        return spinLangue;
    }

    public void setSpinLangue(Spinner spinLangue) {
        this.spinLangue = spinLangue;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_enseigne);
        final Button createEnseigne = (Button) findViewById(R.id.btnregisterenseigne);
        nomEnseigne = (EditText) findViewById(R.id.editnomenseigne);
        loginEditText = (EditText) findViewById(R.id.editloginenseigne);
        passEditText1 = (EditText) findViewById(R.id.editmdpenseigne);
        passEditText2 = (EditText) findViewById(R.id.editverifmdpenseigne);
        descEnseigne = (EditText) findViewById(R.id.editdescritp);
        houverture =  (EditText) findViewById(R.id.edithouvert);
        hfermeture =  (EditText) findViewById(R.id.edithferm);
        spinLangue = (Spinner) findViewById(R.id.spinner1);



        createEnseigne.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {

                if (loginEditText.getText().toString().isEmpty() ||
                        (passEditText1.getText().toString().isEmpty() || passEditText2.getText().toString().isEmpty())) {
                    Toast.makeText(CreateEnseigneActivity.this, "Please fill all required fields", Toast.LENGTH_LONG).show();
                }else if(!passEditText1.getText().toString().contentEquals(passEditText2.getText().toString())){
                    Toast.makeText(CreateEnseigneActivity.this, "Password verification does not match", Toast.LENGTH_LONG).show();
                }
                else {

                    inserer(v);
                }
            }

        });

    }//Fin on create


    public void inserer(View v) {

        userBD = new UtilisateurBD(this);
        langueBD = new LangueBD(this);
        user = new Enseigne(loginEditText.getText().toString(),passEditText1.getText().toString(),nomEnseigne.getText().toString(),descEnseigne.getText().toString(),houverture.getText().toString(),hfermeture.getText().toString());

        userBD.open();
        langueBD.open();
        int idLangue = langueBD.recupIdLangue(spinLangue.getSelectedItem().toString());
        userBD.insertEnseigne(user);
        langueBD.insererLangueEnseigne(loginEditText.getText().toString(), idLangue);
        userBD.close();
        langueBD.close();

        Toast.makeText(this, "Vous venez de vous inscrire en tant que " + loginEditText.getText().toString(), Toast.LENGTH_SHORT).show();
        CreateEnseigneActivity.this.finish();
    }






}
